$(function(){
    for(var i=0;i<$("td[data-goods=id]").length;i++){
        $("input[name=deletegoods]")[i].index = i;
        $("input[name=deletegoods]")[i].onclick = function(){
            $.ajax({
                url:"/api/deletegoods",
                method: "POST",
                data:{
                    goodsid:$("td[data-goods=id]")[this.index].innerHTML
                },
                success: function(data){
                    if(data.status == 0){
                        location.reload();
                    }else{
                        myalert(data.message);
                    }
                },
                error: function(error){
                    console.log(error)
                }
            })
            
        }
    }

    var _goodsid = "";
    for(var i=0;i<$("td[data-goods=id]").length;i++){
        $("input[name=changegoods]")[i].index = i;
        $("input[name=changegoods]")[i].onclick = function(){
            _goodsid = $("td[data-goods=id]")[this.index].innerHTML;
            $.ajax({
                url:"/api/getgoods",
                method: "POST",
                data:{
                    goodsid:_goodsid
                },
                success: function(data){
                    if(data.status == 0){
                        $("input[name=changename]")[0].value = data.goodsdata.goodsname;
                        $("input[name=changeprice]")[0].value = data.goodsdata.price;
                        $("input[name=changedes]")[0].value = data.goodsdata.des;
                        $("#changealert")[0].style.display = "block";
                    }
                },
                error: function(error){
                    console.log(error)
                }
            })
        }
    }

    $("input[name=change]")[0].onclick = function(){
        $.ajax({
            url:"/api/changegoods",
            method: "POST",
            data:{
                goodsid:_goodsid,
                goodsname:$("input[name=changename]")[0].value,
                price:$("input[name=changeprice]")[0].value,
                des:$("input[name=changedes]")[0].value
            },
            success: function(data){
                if(data.status == 0){
                    location.reload()
                }
            },
            error: function(error){
                console.log(error)
            }
        })
    }

    $(".close").click(function(){
        $("#changealert")[0].style.display = "none";
    })

    $("input[name=close]").click(function(){
        $("#changealert")[0].style.display = "none";
    })
})

function myalert(str){
    if(document.getElementsByClassName("my-alert").length == 0){
        new Promise(function(resolve,reject){
            var odiv = document.createElement("div");
            odiv.className = "my-alert";
            odiv.innerHTML = str;
            document.body.appendChild(odiv);
            resolve(odiv)
        }).then(function(odiv){
            var outtimer = setTimeout(function(){
                odiv.remove();
                clearTimeout(outtimer);
            },1000);
        })
    }
}